package com.suneast.suneaststore.modules.personal;

import com.alibaba.fastjson.JSONObject;
import com.suneast.suneaststore.API;
import com.suneast.suneaststore.app.AccountManager;
import com.suneast.suneaststore.modules.common.CommonUtil;
import com.suneast.suneaststore.util.log.LatteLogger;
import com.suneast.suneaststore.util.net.RestClient;

import org.greenrobot.eventbus.EventBus;

public class PersonHandler {
    public static void requestPersonInfo() {
        RestClient.builder()
                .url(API.USER_INFO)
                .params("token", AccountManager.getToken())
                .success(response -> {
                    LatteLogger.json("test", response);
                    JSONObject dataObj = CommonUtil.parseDataFromRootJSON(response);
                    if (dataObj != null) {
                        EventBus.getDefault().post(new GotUserInfo(
                                dataObj.getString("username"),
                                dataObj.getString("mobile"),
                                dataObj.getString("integral"),
                                dataObj.getDoubleValue("balance"),
                                dataObj.getDoubleValue("payment_for_goods"),
                                dataObj.getIntValue("level"),
                                dataObj.getString("header_img")
                        ));
                    }
                })
                .error((code, msg) -> {

                })
                .failure(() -> {

                })
                .build()
                .post();
    }
}
